/**
 * Created by yunpeng on 2014/12/4.
 */
$('#selfDriving').datagrid({
    url:'/circle/sd/sdList',
    //border : false,
    rownumbers: true,
    idField : 'id',
    pagination : true,
    sortName : 'createtime',//默认排序的列
    sortOrder: 'desc',
    pageSize : 10,
    pageList : [ 10, 20, 40, 80 ],
    fitColumns : true,
    fit : true,
    checkOnSelect : false,
    selectOnCheck : true,
    striped: true,//条纹交替
    columns:[[
        {field:'',checkbox:true},
        {field:'title',title:'标题',width:100,align:'center'},
        /*{field:'content',title:'内容',width:100,align:'center'},*/
        {field:'price',title:'价格',width:100,align:'center'},
        {field:'starttime',title:'开始时间',width:100,align:'center'},
        {field:'endtime',title:'结束时间',width:100,align:'center'},
        {field:'count',title:'报名人数',width:100,align:'center'},
        {field:'city',title:'城市',width:100,align:'center'},
        {field:'province',title:'省份',width:100,align:'center'},
        {field:'createtime',title:'创建时间',width:200,align:'center'}
    ]],
    toolbar : '#sdtb'
});


function searchSdList(){
    $('#selfDriving').datagrid('load',{
        ccDate1: $('#sdDate1').datebox('getValue'),
        ccDate2: $('#sdDate2').datebox('getValue'),
        ccName: $('#sdName').textbox('getValue')
    });
}

function delSd(){
    var rows = $('#selfDriving').datagrid('getChecked');
    if (rows.length > 0) {
        $.messager.confirm('确认', '是否删除勾选的商家？', function(b) {
            if (b) {
                var ids = [];
                var imgs = [];
                for ( var i = 0; i < rows.length; i++) {
                    ids.push(rows[i].id);
                    imgs.push(rows[i].pic);
                }
                $.ajax({
                    url : '/circle/auth/sd/delSd',
                    data : {
                        ids : ids.join(),
                        imgs: imgs.join()
                    },
                    type:'POST',
                    dataType : 'json',
                    success : function(data) {
                        if(data==true){
                            $('#selfDriving').datagrid('reload');
                            $('#selfDriving').datagrid('uncheckAll');// 取消选中的所有记录
                            showMsg('删除成功！');
                        }else{
                            showMsg('删除失败！');
                        }
                    }
                });
            }
        });

    } else {
        $.messager.alert('提示', '请勾选要删除的商家。', 'error');
    }
}

function editSd(){//编辑商家信息
    var rows = $('#selfDriving').datagrid('getChecked');
    if (rows.length == 1) {
        //动态加载
        var editCarClubWindow = $('<div/>').dialog(
            {
                width : 800,
                height : 520,
                href : '../circle/selfdrivingEdit.html',
                modal : true,
                title : '编辑自驾游信息',
                maximizable:true,
                cache:false,
                minimized:true,
                buttons : [{
                    text : '取&nbsp;&nbsp;消',
                    width : 60,
                    handler : function() {
                        editCarClubWindow.dialog('close');
                    }
                },
                    {
                        text : '保&nbsp;&nbsp;存',
                        width : 60,
                        handler : function() {
                            $('#editSd').form('submit', {
                                url:'/circle/auth/sd/editSd',
                                onSubmit: function(){
                                    return $(this).form('validate');
                                },
                                success:function(data){
                                    if(data=='true'){
                                        editCarClubWindow.dialog('close');
                                        $('#selfDriving').datagrid('load');
                                        showMsg('修改成功！');
                                    }else{
                                        showMsg('修改失败！');
                                    }
                                }
                            });
                        }
                    } ],
                onClose : function() {
                    ue.destroy();
                    $('#selfDriving').datagrid('uncheckAll');// 取消选中的所有记录
                    $(this).dialog('destroy');//动态加载一定要销毁
                },
                //加载数据，用onLoad，不是用onOpen
                onLoad : function() {

                    //ue.setContent(rows[0].content, false);
                    $('#editSd').form('load', rows[0]);
                    //$('#content').text(rows[0].content);
                    $('#filePath').filebox({
                        buttonText: '请选择',
                        buttonAlign: 'right'
                    });
                    //loadselect(rows[0].cid,rows[0].stypeid);

                    $('#cid').combobox('select',rows[0].cid);

                    $("#imgs").attr("src",rows[0].pic);//设置预览
                    //显示商家图片
                    //$('#imgs').html('<img src="'+rows[0].img+'" style="width:100px;height:100px;vertical-align: middle;margin-top: -19px;">&nbsp;&nbsp;(保存后更改)');
                }
            });
    } else {
        $.messager.alert('提示', '请选择1条记录', 'error');
    }
}


function addSd(){//添加商家
    var addWindow = $('<div/>').dialog({
        width : 800,
        height : 520,
        href : '../circle/selfdrivingAdd.html',
        modal : true,
        title : '信息发布',
        //minimizable:true,
        maximizable:true,
        cache:false,
        minimized:true,
        buttons : [ {
            text : '取&nbsp;&nbsp;消',
            width : 60,
            handler : function() {
                addWindow.dialog('close');
            }
        }, {
            text : '保&nbsp;&nbsp;存',
            width : 60,
            handler : function() {
                $('#addSd').form('submit', {
                    url:'/circle/auth/sd/addSd',
                    onSubmit: function(){
                        return $(this).form('validate');
                    },
                    success:function(data){
                        if(data=='true'){
                            addWindow.dialog('close');
                            $('#selfDriving').datagrid('load');
                            showMsg('添加成功！');
                        }else{
                            showMsg('添加失败！');
                        }
                    }
                });

            }
        } ],
        onLoad : function(){
            /*$('#filePath').filebox({
                buttonText: '请选择',
                buttonAlign: 'right'
            });*/
            //加载下拉菜单
            //loadselect();
        },
        onClose : function() {
            ue.destroy();
            $(this).dialog('destroy');//动态加载一定要销毁
        }
    });

}

function sdOrders(){
    sdEditing = getCheckedOne('#selfDriving');
    if(sdEditing){
        var id = 'sdOrdersTab-'+sdEditing.id;
        openIdTab({id:id,title:'报名订单信息',href:'/circle/sdorders.html',closable:true});
    }

}

function showMsg(msg){
    $.messager.show({
        title:'提示',
        msg:msg,
        timeout:2000,
        showType:'fade'
    });

}

$.extend($.fn.validatebox.defaults.rules,{
    number:{
        validator:function(value, param){
            var reg = /^\d+\.?\d*$/;
            return reg.test(value);
        },
        message:'请输入有效数字'
    }
})
